/**
 * Created by zoushuiyun on 2016/12/26.
 */
import {Pipe, PipeTransform} from '@angular/core';
import {isNullOrUndefined} from 'util';
import {SEX_OPTION, YESNO_OPTION} from '../../constant/dictionary';
import {formLongDate} from '../../core/util/date-utils';
import {myValidNull} from '../../core/util/string-utils';


/**
 * 性别，从 dictionary 查找
 * one to one
 */
@Pipe({name: 'sexPipe'})
export class SexPipe implements PipeTransform {
  constructor() {
  }

  transform(value: number): string {
    let result = '';
    const sexDicts: any[] = SEX_OPTION;
    for (const sex of sexDicts) {
      if (sex.key === value) {
        result = sex.value;
        break;
      }
    }
    return result;
  }
}

@Pipe({name: 'longDatePipe'})
export class LongDatePipe implements PipeTransform {
  constructor() {
  }

  transform(value: number): string {
    return formLongDate(value);
  }
}


/**
 * 1是2否状态，从 dictionary 查找
 * one to one
 */
@Pipe({name: 'yesNoPipe'})
export class YesNoPipe implements PipeTransform {
  constructor() {
  }

  transform(value: number): string {
    let result = '';
    const yesNoDicts: any[] = YESNO_OPTION;
    for (const o of yesNoDicts) {
      if (o.key === value) {
        result = o.value;
        break;
      }
    }
    return result;
  }
}


/**
 * 通用翻译器  字典表或后台获取的字典
 *
 *
 *  one to one
 */
@Pipe({name: 'dicsNamePipe'})
export class DicsNamePipe implements PipeTransform {
  constructor() {
  }

  transform(value: string, groupDicts: any[], key?: any, desc?: any): string {
    let keyName: any = 'key';
    let descName: any = 'value';
    if (!myValidNull(key)) {
      keyName = key;
    }
    if (!myValidNull(desc)) {
      descName = desc;
    }
    let result = value;
    for (const obj of groupDicts) {
      if (obj[keyName] === value) {
        result = obj[descName];
        break;
      }
    }
    return result;
  }
}


/**
 * 字符串截取
 */
@Pipe({name: 'stringCutPipe'})
export class StringCutPipe implements PipeTransform {
  constructor() {
  }

  transform(value: string, args: number): string {
    if (isNullOrUndefined(value)) {
      return '';
    }
    if (value.length <= args) {
      return value;
    } else {
      return value.substring(0, args);
    }

  }
}

/**
 * 字符串截取 带。。。
 */
@Pipe({name: 'stringEllipsisCutPipe'})
export class StringEllipsisCutPipe implements PipeTransform {
  constructor() {
  }

  transform(value: string, args: number): string {
    if (isNullOrUndefined(value)) {
      return '';
    }
    if (value.length <= args) {
      return value;
    } else {
      return value.substring(0, args) + '...';
    }

  }
}

/**
 * 过滤器
 */
@Pipe({name: 'filterPipe', pure: false})
export class FilterPipe implements PipeTransform {
  transform(array: Array<any>, filterName: string, filterValue: any): Array<any> {
    const result: Array<any> = new Array;
    array.forEach((arr) => {
      if (arr[filterName] === filterValue) {
        result.push(arr);
      }
    });
    return result;
  }
}

/**
 * 将arr中selected为true的元素排在前面 ---可自行扩展
 */
@Pipe({name: 'selectedPipe', pure: false})
export class SelectedPipe implements PipeTransform {
  transform(array: Array<any>, args: string): Array<any> {
    const result: Array<any> = new Array;
    array.forEach((arr) => {
      if (arr.selected) {
        result.push(arr);
      }
    });
    array.forEach((arr) => {
      if (!arr.selected) {
        result.push(arr);
      }
    });
    return result;
  }
}
